#include <iostream>
#include <sstream>
#include "CTruthDataPoint.h"

using namespace std;
/********************************************************************/
CTruthDataPoint::CTruthDataPoint( void )
{
	m_inFileName = "";
	m_outFileName = "";
	m_size = cvSize(0,0);
	m_center=cvPoint(0,0);
	m_TR = cvPoint(0,0);
	m_BL = cvPoint(0,0); 
	m_img = NULL; 
}
/********************************************************************/
CTruthDataPoint::CTruthDataPoint( string source_file, string dest_file, double classification, CvSize sz, CvPoint pt )
{
	m_inFileName = source_file;
	m_outFileName = dest_file;
	m_classification.SetTruth(classification);
	m_size = sz;
	m_center=pt;
	m_TR = cvPoint(0,0);
	m_BL = cvPoint(0,0); 
	m_img = NULL; 
}
/********************************************************************/
CTruthDataPoint::CTruthDataPoint( string file )
{
	m_inFileName = file;
	m_img = NULL;
	/*if( LoadSourceImage() )
	{
		m_size = cvSize(m_img->width, m_img->height);
		m_center = cvPoint(m_img->width/2, m_img->height/2);
	}*/
	m_outFileName = file;
	m_TR = cvPoint(0,0);
	m_BL = cvPoint(0,0); 
}
/********************************************************************/
CTruthDataPoint::~CTruthDataPoint( void )
{
	if( NULL != m_img )
	{
		cvReleaseImage( &m_img );
	}
}
/********************************************************************/
CTruthDataPoint::CTruthDataPoint( const CTruthDataPoint& other )
{
	m_inFileName = other.m_inFileName;
	m_outFileName = other.m_outFileName;
	m_size = other.m_size;
	m_center= other.m_center;
	m_TR = other.m_TR;
	m_BL = other.m_BL;
	m_classification = other.m_classification; 
	if( NULL != m_img )
	{
		cvReleaseImage(&m_img);
		m_img = NULL;
	}
	if( other.m_img != NULL )
	{
		CvSize sz;
		sz.width = other.m_img->width;
		sz.height = other.m_img->height;
		m_img = cvCreateImage(sz,other.m_img->depth,m_img->nChannels); 
		cvCopy(other.m_img,m_img); 
	}
}
/********************************************************************/
CTruthDataPoint& CTruthDataPoint::operator= (const CTruthDataPoint& other)
{
	if( this != &other )
	{
		m_inFileName = other.m_inFileName;
		m_outFileName = other.m_outFileName;
		m_size = other.m_size;
		m_center= other.m_center;
		m_TR = other.m_TR;
		m_BL = other.m_BL;
		m_classification = other.m_classification; 
		if( NULL != m_img )
		{
			cvReleaseImage(&m_img);
			m_img = NULL;
		}
		if( other.m_img != NULL )
		{
			CvSize sz;
			sz.width = other.m_img->width;
			sz.height = other.m_img->height;
			m_img = cvCreateImage(sz,other.m_img->depth,m_img->nChannels); 
			cvCopy(other.m_img,m_img); 
		}
	}
	return *this;
}
/********************************************************************/
bool CTruthDataPoint::ReadFromLine( string line )
{
	istringstream iss(line, istringstream::in);
	double temp; 
	iss >> m_inFileName;
	iss >> m_outFileName;
	iss >> temp;
	m_classification.SetTruth(temp); 
	int x=0,y=0,w=0,h=0;
	iss >> x >> y >> w >> h;
	m_size = cvSize(w,h);
	m_center = cvPoint(x,y);
	int x0=0,y0=0,x1=0,y1=0;
	iss >> x0 >> y0 >> x1 >> y1;
	m_TR.x = x0;
	m_TR.y = y0;
	m_BL.x = x1;
	m_BL.y = y1;
	m_TR = cvPoint(x0,y0);
	m_BL = cvPoint(x1,y1);
	return( iss.good() );
}
/********************************************************************/
string CTruthDataPoint::PrintToLine( void )
{
	ostringstream oss;
	oss << m_inFileName <<"\t" << m_outFileName << "\t" << m_classification.GetTruth() << "\t"
		<< m_center.x << "\t" << m_center.y << "\t" << m_size.width << "\t" << m_size.height;
	return oss.str();
}
/********************************************************************/
bool CTruthDataPoint::LoadSourceImage( void )
{
	bool retVal = false;
	if( m_img == NULL )
	{
		m_img = cvLoadImage(m_inFileName.c_str() );
		if( m_img != NULL)
			retVal = true;
	}
	return retVal;
}
/********************************************************************/
bool CTruthDataPoint::FreeSourceImage( void )
{
	bool retVal = false;
	if( m_img != NULL )
	{
		cvReleaseImage( &m_img );
		m_img = NULL;
		retVal = true;
	}
	return retVal;
}
/********************************************************************/
bool CTruthDataPoint::HasImage( void )
{
	return( m_img != NULL );
}
/********************************************************************/
IplImage * CTruthDataPoint::GetImage( void )
{
	return m_img;
}
/********************************************************************/
void CTruthDataPoint::SetImage( IplImage * img  ) 
{
	if( m_img != NULL )
	{
		FreeImage();
	}
	m_img = img;
}
/********************************************************************/
bool CTruthDataPoint::SaveImage( string fname )
{
	bool retVal = false;
	if( NULL != m_img )
	{
		int value = cvSaveImage(fname.c_str(), m_img );
		if( value == 0 )
			retVal = true;
	}
	return retVal; 
}
/********************************************************************/
bool CTruthDataPoint::SaveImage( void )
{
	return SaveImage(m_outFileName);
}
/********************************************************************/
void CTruthDataPoint::FreeImage( void )
{
	if( m_img != NULL )
	{
		cvReleaseImage(&m_img);
		m_img = NULL;
	}
}
/********************************************************************/
string CTruthDataPoint::GetInFile( void )
{
	return m_inFileName;
}
/********************************************************************/
void CTruthDataPoint::SetInFile( string fname )
{
	m_inFileName =fname;
}
/********************************************************************/
string CTruthDataPoint::GetOutFile( void )
{
	return m_outFileName;
}
/********************************************************************/
void CTruthDataPoint::SetOutFile( string fname)
{
	m_outFileName = fname;
}
/********************************************************************/
CvSize CTruthDataPoint::GetSize( void )
{
	return m_size;
}
/********************************************************************/
void CTruthDataPoint::SetSize( CvSize sz )
{
	m_size=sz;
}
/********************************************************************/
void CTruthDataPoint::SetSize( int w, int h)
{
	m_size = cvSize(w,h);
}
/********************************************************************/
CvPoint CTruthDataPoint::GetPoint( void )
{
	return m_center;
}
/********************************************************************/
void CTruthDataPoint::SetPoint( CvPoint pt )
{
	m_center= pt;
}
/********************************************************************/
void CTruthDataPoint::SetPoint( int x, int y )
{
	m_center = cvPoint(x,y);
}
/********************************************************************/
string CTruthDataPoint::Print( void )
{
	ostringstream oss;
	oss << "Source      File: " << m_inFileName << endl;
	oss << "Destination File: " << m_outFileName << endl;
	oss << "Classification  : " << m_classification.GetTruth() << endl;
	return oss.str();
}
/********************************************************************/